<?php

	require_once('jacked_configVars.php');
	require_once('jacked_functions.php');		
	if(!validateLogin()){
		exit();
	}
	require_once('jacked_configpdnet.php');
	require_once('jacked_pdnetfunctions.php');

	function dumpDupeSubmissions(){
		$result = jackedDBQuery('SELECT decision, result, count( * ) cnt FROM Submission GROUP BY decision, result HAVING cnt > 1 ORDER BY cnt DESC');
		while($row = mysql_fetch_array($result)){
			$query2 = "DELETE FROM Submission WHERE `decision` = '" . $row['decision'] . "' AND `result` = '". $row['result'] . "' LIMIT " . ($row['cnt'] - 1);
			$killed = jackedDBQuery($query2);
		}
	}
	
	function getQueueLength(){
		return jackedDBGetVal('function:count(*)', 'Queue');
	}
	
	function getSubmissionsLength($clearDupes = false){
		if($clearDupes)
			dumpDupeSubmissions();
		return jackedDBGetVal('function:count(*)', 'Submission');
	}
	
	function getLengths(){
		return array(
			"queue" 	  => getQueueLength(),
			"submissions" => getSubmissionsLength()
		);
	}
	
	function getSubmissions($howMany = false, $page = false, $clearDupes = true){
		if($clearDupes)
			dumpDupeSubmissions();
			
		$page = '';
		if($howMany){
			if($page){
				$page = paginator($howMany, $page);
			}else{
				$page = ' LIMIT ' . $howMany;
			}
		}
		$submissions = jackedDBGetResult('Submission', "`posted` = '0' ORDER BY id DESC" . $page);
		
		$final = array();
		while($row = mysql_fetch_array($submissions, MYSQL_ASSOC)){
			$final[] = $row;
		}
		
		return $final;
	}
	
	function getSubmissionByID($id){
		return jackedDBGetRow('Submission', "`id` = '$id'");
	}
	
	function deleteSubmissionByID($id){
		return jackedDBDelete('Submission', "`id` = '$id'");
	}
	
	function enqueueSubmission($theid, $thedecision, $theresult, $thetags){
		if($theid && $thedecision && $theresult && $thetags){
			$query="INSERT INTO PoorDecision (`decision`, `result`, `votes`, `date`, `alive`) VALUES ('".mysql_real_escape_string($thedecision)."', '".mysql_real_escape_string($theresult)."', '0', '" . time() . "' '0')";
			$result=mysql_query($query, JACKED_DEFAULT_LINK);
			$pid=mysql_insert_id();
			$qquery="INSERT INTO Queue (`PoorDecision`) VALUES ('".$pid."')";
			$qresult=mysql_query($qquery, JACKED_DEFAULT_LINK);

			$query4="UPDATE Submission SET `postID` = '".$pid."', `posted`= 1  WHERE id='".$theid."'";
			$result4=mysql_query($query4, JACKED_DEFAULT_LINK);
				
			foreach(explode(",", $thetags) as $tag){
				$tag = trim($tag);//MOTHERFUCKER
				$query="SELECT id FROM Tag where name='".mysql_real_escape_string($tag)."'";
				$result=mysql_query($query, JACKED_DEFAULT_LINK);
				$num=mysql_numrows($result);
				if ($num == 0){
					$query="INSERT INTO Tag (`usage`, `name`, `slug`) VALUES ('1', '".mysql_real_escape_string($tag)."','".slugify($tag)."')";
					$result=mysql_query($query, JACKED_DEFAULT_LINK);
					$tid=mysql_insert_id();
					$query2="INSERT INTO TagRel VALUES ('".$pid."','".$tid."')";
					$result2=mysql_query($query2, JACKED_DEFAULT_LINK);
					$result=NULL;
				}else{
					$trow=mysql_fetch_array($result);
					$tid=$trow[0];
					$query2="INSERT INTO TagRel VALUES ('".$pid."','".$tid."')";
					$result2=mysql_query($query2, JACKED_DEFAULT_LINK);
					$result=NULL;
					$query3="UPDATE Tag SET `usage`=`usage`+1 WHERE id='".$tid."'";
					$result3=mysql_query($query3, JACKED_DEFAULT_LINK);
				}

			}
		
			return $pid;
			
		}else{
			return false;
		}
	}
	
?>